package com.ang.filter.filters.filterRegistrationBean;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;

@Configuration
public class MyFilterConfig {

    @Bean("myFilterRegistrationBean1")
    public FilterRegistrationBean<MyFilterRegistrationBean1> myFilterRegistrationBean1(){
        FilterRegistrationBean<MyFilterRegistrationBean1> filter = new FilterRegistrationBean();

        filter.setOrder(4);
        filter.setFilter(new MyFilterRegistrationBean1());
        filter.setName("myFilterRegistrationBean1");

        return filter;
    }

    @Bean("myFilterRegistrationBean2")
    public FilterRegistrationBean<MyFilterRegistrationBean2> myFilterRegistrationBean2(){
        FilterRegistrationBean<MyFilterRegistrationBean2> filter = new FilterRegistrationBean();

        filter.setOrder(3);
        filter.setFilter(new MyFilterRegistrationBean2());
        filter.setName("myFilterRegistrationBean2");
        //DispatcherType.ERROR：错误跳转资源(比如：404，500)，正常情况下不会走这个配置的拦截器：MyFilterRegistrationBean2
        filter.setDispatcherTypes(DispatcherType.ERROR);
        //404最终会被从定向到error，然后进入Bean2的过滤器，即：这个拦截器只拦截/error的请求
        filter.addUrlPatterns("/error");

        return filter;
    }
}
